<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 分类属性
 */

class Attribute_mdl extends MY_Model{

	public function __construct() {
        parent::__construct();
        $this->table = 'sort_attribute';
    }
	
	//递归专用
	static public $treeList = array();
	
	
	//查询当前分类的所有属性
	public function get_all($sid){
		$this->db->where('sort_id',$sid);
		$this->db->order_by('id asc');
		return $this->db->get($this->table)->result_array();
	}
	
	//根据条件查询属性
	public function select($map=array()){
		foreach($map as $k => $v){
			$this->db->where($k,$v);
		}
		$this->db->order_by('id asc');
		return $this->db->get($this->table)->result_array();
	}
	
	/**
	 * 属性添加
	 */
	public function install($data){
		
		$data['create_time'] = $_SERVER['REQUEST_TIME'];
		$data['user_id'] = $this->session->userdata('id');
		
		return $this->db->insert($this->table,$data);
	}
	
	/**
	 * 修改
	 */
	public function update($data){
		$data['create_time'] = $_SERVER['REQUEST_TIME'];
		$data['user_id'] = $this->session->userdata('id');
		return $this->db->update($this->table,$data);
	}
	
	/**
	 * 属性删除
	 */
	public function delete($map=array()){
		foreach($map as $k => $v){
			$this->db->where($k,$v);
		}
		return $this->db->delete($this->table);
	}
	
	/**
	 * 属性组装
	 */
	public function get_list($data,$key = 0){
		
		$tree = [];
		foreach($data as $k => $v){
			if($v['pid'] == $key){
				$v['tree'] = $this->get_list($data,$v['id']);
				$tree[] = $v;
				unset($data[$k]);
			}
		}
		return $tree;
	}
}